package jzbank.service.impl;

import jzbank.common.exception.SyncException;
import jzbank.dao.serializabledao.DataExportDao;
import jzbank.dao.serializabledao.FTPInstanceDao;
import jzbank.pojo.comfig.FTPInstancePO;
import jzbank.pojo.dto.DataExportDTO;
import jzbank.service.FTPInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class FTPInstanceServiceImpl implements FTPInstanceService {

    @Autowired
    private FTPInstanceDao ftpInstanceDao;

    @Autowired
    private DataExportDao dataExportDao;

    @Override
    public void createFTPConfig(FTPInstancePO ftpInstancePO) {
        ftpInstancePO.setFid(UUID.randomUUID().toString());
        // 在此编写添加FTP配置的逻辑
        ftpInstanceDao.addFTPInstancePo(ftpInstancePO);
    }

    @Override
    public void deleteFTPConfigById(String id) {
        // 检查是否有任务正在使用该FTP配置
        List<DataExportDTO> allDataExport = dataExportDao.getAllDataExport();
        for (DataExportDTO dataExportDTO : allDataExport) {
            if (dataExportDTO.getFid() != null && dataExportDTO.getFid().equals(id)) {
                throw new SyncException("这个FTP配置信息正在被任务使用，不允许删除");
            }
        }
        ftpInstanceDao.deleteFTPInstancePo(id);
    }
    @Override
    public FTPInstancePO selectFTPConfigById(String id) {
        return ftpInstanceDao.getFTPInstancePoByFid(id);
    }

    @Override
    public List<FTPInstancePO> selectAllFTPConfigs() {
        return ftpInstanceDao.getAllFTPInstances();
    }

    @Override
    public void updateFTPConfig(FTPInstancePO ftpInstancePO) {
        ftpInstanceDao.updateFTPInstancePo(ftpInstancePO);
    }
}